#!/bin/bash

# Test script for AI endpoint configurations
# Each test sends a simple chat completion request to verify the endpoint is working

echo "Testing AI Endpoints Configuration"
echo "==================================="

# Test deepseek-chat (raw)
echo -e "\n[1/13] Testing deepseek-chat (raw)..."
response=$(curl -s -w "\n---STATUS:%{http_code}---" \
  -X POST https://api.deepseek.com/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-3173cd82e38244dd90e8d4cb7d472821" \
  -d '{
    "model": "deepseek-chat",
    "messages": [{"role": "user", "content": "Hello, what is your model name?"}],
    "max_tokens": 1024
  }')
status=$(echo "$response" | grep -o "STATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed 's/---STATUS:[0-9]*---//')
echo "Status: $status"
echo "Response: $body" 
echo ""

# Test deepseek-reasoner (raw)
echo -e "\n[2/13] Testing deepseek-reasoner (raw)..."
response=$(curl -s -w "\n---STATUS:%{http_code}---" \
  -X POST https://api.deepseek.com/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-3173cd82e38244dd90e8d4cb7d472821" \
  -d '{
    "model": "deepseek-reasoner",
    "messages": [{"role": "user", "content": "Hello, what is your model name?"}],
    "max_tokens": 1024
  }')
status=$(echo "$response" | grep -o "STATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed 's/---STATUS:[0-9]*---//')
echo "Status: $status"
echo "Response: $body" 
echo ""

# Test gpt-4o (azure)
echo -e "\n[3/13] Testing gpt-4o (azure)..."
response=$(curl -s -w "\n---STATUS:%{http_code}---" \
  -X POST "https://sfgptapi-east-us.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview" \
  -H "Content-Type: application/json" \
  -H "api-key: 6tKCRQfF5NU3FIFTQWFfpDgJF4RXJP5i1nPzuLeJBPMYVKLSAJdfJQQJ99BBACYeBjFXJ3w3AAABACOGBaSE" \
  -d '{
    "messages": [{"role": "user", "content": "Hello, what is your model name?"}],
    "max_tokens": 1024
  }')
status=$(echo "$response" | grep -o "STATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed 's/---STATUS:[0-9]*---//')
echo "Status: $status"
echo "Response: $body" 
echo ""

# Test gpt-4o-mini (azure)
echo -e "\n[4/13] Testing gpt-4o-mini (azure)..."
response=$(curl -s -w "\n---STATUS:%{http_code}---" \
  -X POST "https://sfgptapi-east-us.openai.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2025-01-01-preview" \
  -H "Content-Type: application/json" \
  -H "api-key: 6tKCRQfF5NU3FIFTQWFfpDgJF4RXJP5i1nPzuLeJBPMYVKLSAJdfJQQJ99BBACYeBjFXJ3w3AAABACOGBaSE" \
  -d '{
    "messages": [{"role": "user", "content": "Hello, what is your model name?"}],
    "max_tokens": 1024
  }')
status=$(echo "$response" | grep -o "STATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed 's/---STATUS:[0-9]*---//')
echo "Status: $status"
echo "Response: $body" 
echo ""

# Test o3-mini (azure)
echo -e "\n[5/13] Testing o3-mini (azure)..."
response=$(curl -s -w "\n---STATUS:%{http_code}---" \
  -X POST "https://sfacg-m6yrbdg7-eastus2.cognitiveservices.azure.com/openai/deployments/o3-mini/chat/completions?api-version=2025-01-01-preview" \
  -H "Content-Type: application/json" \
  -H "api-key: FrS927wLKLBrzNeyAmkfPdu4zyixx7yUf5MbZAWCYUeIrnxuWFL9JQQJ99BBACHYHv6XJ3w3AAAAACOGqd3o" \
  -d '{
    "messages": [{"role": "user", "content": "Hello, what is your model name?"}],
    "max_completion_tokens": 1024
  }')
status=$(echo "$response" | grep -o "STATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed 's/---STATUS:[0-9]*---//')
echo "Status: $status"
echo "Response: $body" 
echo ""

# Test gemini-2.5-flash (closeai)
echo -e "\n[6/13] Testing gemini-2.5-flash (closeai)..."
response=$(curl -s -w "\n---STATUS:%{http_code}---" \
  -X POST https://api.openai-proxy.org/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-rX7lYlaDeYhZOHVAPTFXTSTi9m98c53ay01lHYR6i7roTPTc" \
  -d '{
    "model": "gemini-2.5-flash",
    "messages": [{"role": "user", "content": "Hello, what is your model name?"}],
    "max_tokens": 1024
  }')
status=$(echo "$response" | grep -o "STATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed 's/---STATUS:[0-9]*---//')
echo "Status: $status"
echo "Response: $body" 
echo ""

# Test gemini-2.5-pro (closeai)
echo -e "\n[7/13] Testing gemini-2.5-pro (closeai)..."
response=$(curl -s -w "\n---STATUS:%{http_code}---" \
  -X POST https://api.openai-proxy.org/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-rX7lYlaDeYhZOHVAPTFXTSTi9m98c53ay01lHYR6i7roTPTc" \
  -d '{
    "model": "gemini-2.5-pro",
    "messages": [{"role": "user", "content": "Hello, what is your model name?"}],
    "max_tokens": 1024
  }')
status=$(echo "$response" | grep -o "STATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed 's/---STATUS:[0-9]*---//')
echo "Status: $status"
echo "Response: $body" 
echo ""

# Test gpt-5-2025-08-07 (closeai)
echo -e "\n[8/13] Testing gpt-5-2025-08-07 (closeai)..."
response=$(curl -s -w "\n---STATUS:%{http_code}---" \
  -X POST https://api.openai-proxy.org/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-rX7lYlaDeYhZOHVAPTFXTSTi9m98c53ay01lHYR6i7roTPTc" \
  -d '{
    "model": "gpt-5-2025-08-07",
    "messages": [{"role": "user", "content": "Hello, what is your model name?"}],
    "max_completion_tokens": 1024
  }')
status=$(echo "$response" | grep -o "STATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed 's/---STATUS:[0-9]*---//')
echo "Status: $status"
echo "Response: $body" 
echo ""

# Test gpt-5-mini (closeai)
echo -e "\n[9/13] Testing gpt-5-mini (closeai)..."
response=$(curl -s -w "\n---STATUS:%{http_code}---" \
  -X POST https://api.openai-proxy.org/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-rX7lYlaDeYhZOHVAPTFXTSTi9m98c53ay01lHYR6i7roTPTc" \
  -d '{
    "model": "gpt-5-mini",
    "messages": [{"role": "user", "content": "Hello, what is your model name?"}],
    "max_completion_tokens": 1024
  }')
status=$(echo "$response" | grep -o "STATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed 's/---STATUS:[0-9]*---//')
echo "Status: $status"
echo "Response: $body" 
echo ""

# Test o4-mini (closeai)
echo -e "\n[10/13] Testing o4-mini (closeai)..."
response=$(curl -s -w "\n---STATUS:%{http_code}---" \
  -X POST https://api.openai-proxy.org/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-rX7lYlaDeYhZOHVAPTFXTSTi9m98c53ay01lHYR6i7roTPTc" \
  -d '{
    "model": "o4-mini",
    "messages": [{"role": "user", "content": "Hello, what is your model name?"}],
    "max_completion_tokens": 1024
  }')
status=$(echo "$response" | grep -o "STATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed 's/---STATUS:[0-9]*---//')
echo "Status: $status"
echo "Response: $body" 
echo ""

# Test claude-sonnet-4-20250514 (closeai)
echo -e "\n[11/13] Testing claude-sonnet-4-20250514 (closeai)..."
response=$(curl -s -w "\n---STATUS:%{http_code}---" \
  -X POST https://api.openai-proxy.org/anthropic/v1/messages \
  -H "Content-Type: application/json" \
  -H "x-api-key: sk-rX7lYlaDeYhZOHVAPTFXTSTi9m98c53ay01lHYR6i7roTPTc" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-sonnet-4-20250514",
    "messages": [{"role": "user", "content": "Hello, what is your model name?"}],
    "max_tokens": 1024
  }')
status=$(echo "$response" | grep -o "STATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed 's/---STATUS:[0-9]*---//')
echo "Status: $status"
echo "Response: $body" 
echo ""

# Test claude-opus-4-1-20250805 (closeai)
echo -e "\n[12/13] Testing claude-opus-4-1-20250805 (closeai)..."
response=$(curl -s -w "\n---STATUS:%{http_code}---" \
  -X POST https://api.openai-proxy.org/anthropic/v1/messages \
  -H "Content-Type: application/json" \
  -H "x-api-key: sk-rX7lYlaDeYhZOHVAPTFXTSTi9m98c53ay01lHYR6i7roTPTc" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-opus-4-1-20250805",
    "messages": [{"role": "user", "content": "Hello, what is your model name?"}],
    "max_tokens": 1024
  }')
status=$(echo "$response" | grep -o "STATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed 's/---STATUS:[0-9]*---//')
echo "Status: $status"
echo "Response: $body" 
echo ""

# Test qwen-max (aliyuncs)
echo -e "\n[13/13] Testing qwen-max (aliyuncs)..."
response=$(curl -s -w "\n---STATUS:%{http_code}---" \
  -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-fe7b5366c70f407c9ebed6feb7a722de" \
  -d '{
    "model": "qwen-max",
    "messages": [{"role": "user", "content": "Hello, what is your model name?"}],
    "max_tokens": 1024
  }')
status=$(echo "$response" | grep -o "STATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed 's/---STATUS:[0-9]*---//')
echo "Status: $status"
echo "Response: $body" 
echo ""

echo -e "\n==================================="
echo "Test completed!"
echo ""
echo "Status codes:"
echo "  200 - Success"
echo "  401 - Authentication failed (check API key)"
echo "  403 - Forbidden (check permissions)"
echo "  404 - Endpoint not found"
echo "  429 - Rate limited"
echo "  500+ - Server error"